package com.psp.web.persist.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.psp.web.model.UserBean;
import com.psp.web.model.UserLoginLogBean;
import com.psp.web.model.UserUpdateLogBean;
import com.psp.web.persist.dao.UserDao;

@Repository
public class UserImpl extends BaseImpl implements UserDao {

	final String NAME_SPACE = NAME_SPACE_HEADER + ".UserMapper";

	public int insert(UserBean user) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".insert", user);
	}

	public UserBean selectOneByPhone(String phone) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectOneByPhone", phone);
	}

	public UserBean selectOneByEmail(String email) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectOneByEmail", email);
	}

	public UserBean selectOne(String uid) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectOneByUid", uid);
	}

	public int updateState(String userId, int state) {
		Map<String, Object> params = new HashMap<>();
		params.put("uid", userId);
		params.put("state", state);
		return sqlSessionTemplate.update(NAME_SPACE + ".updateState", params);
	}

	public int updateLoginTime(String uid) {
		return sqlSessionTemplate.update(NAME_SPACE + ".updateLoginTime", uid);
	}

	@Override
	public int updatePwd(UserBean user) {
		Map<String, Object> params = new HashMap<>();
		params.put("uid", user.getUid());
		params.put("password", user.getPassword());
		return sqlSessionTemplate.update(NAME_SPACE + ".updatePwd", params);
	}

	@Override
	public int updateUser(UserBean user) {
		Map<String, Object> params = new HashMap<>();
		params.put("uid", user.getUid());
		params.put("nickName", user.getNickName());
		params.put("headIcon", user.getHeadIcon());
		params.put("phone", user.getPhone());
		params.put("email", user.getEmail());
		params.put("idNo", user.getIdno());
		params.put("gender", user.getGender());
		return sqlSessionTemplate.update(NAME_SPACE + ".updateUser", params);
	}

	@Override
	public int insertLoginLog(UserLoginLogBean userLoginLog) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".insertLoginLog", userLoginLog);
	}

	@Override
	public int insertUpdateLog(UserUpdateLogBean userUpdateLog) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".insertUpdateLog", userUpdateLog);
	}

	@Override
	public UserBean selectActiveUserByEmail(String email) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectActiveUserByEmail", email);
	}

}
